import serverData from './serverData.js'
import localMap from './localMap'
// 配置服务地址
//let host = "http://139.196.41.193/eladmin"
let host = "http://192.168.207.211:8000"

let http = {
	request: function(option, successFunc, failFunc) {
		if (option.loading != "noLoading") {
			uni.showLoading({
				title: '加载中',
				mask: true,
			});
		}
		let requestConfig = {
			url: host + option.url,
			method: option.method || "POST",
			data: option.data || {},
			header: option.header || {
				token: localMap.get("token") || ""
			},
			success: (res) => {
				
				if (option.loading != "noLoading") {
					uni.hideLoading();
				}

				if (res.data.code != '0') {
					if (option.loading != "noLoading") {
						uni.showToast({
							icon: 'none',
							title: res.data.msg,
							duration: 2000
						});
					}
					failFunc(res)
				} else {
					successFunc(res)
				}

			},
			fail: (res) => {
				if (option.loading != "noLoading") {
					uni.hideLoading();
				}
				failFunc(res)
			}
		}
		uni.request(requestConfig);
	},
	filterUrl: function(urlData, url) {
		for (let key in urlData) {
			let keys = "{" + key + "}";
			url = url.replace(keys, urlData[key])
		}
		return url;
	},
}

export default http;
